// pages/tabbar/datas/components/thirdTab.js
import { SignUpModel } from "../../../../api/api-work/signUpModel.js"
import { DataModel } from "../../../../api/api-data/dataModel.js"
import moment from "moment"
const dataModel = new DataModel()
const signUpModel = new SignUpModel()

Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    radioType: "1", // 1: 按天 2：按月
    showStartDate: false,
    minDate: new Date(2020, 0, 1).getTime(),
    startDateLabel: moment().format('YYYY-MM-DD'),
    startDateValue: moment().valueOf(), // 转为毫秒级时间戳
    showEndDate: false,
    endDateLabel: moment().format('YYYY-MM-DD'),
    endDateValue: moment().valueOf(), // 转为毫秒级时间戳
    formatterDate(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return `${value}日`;
    },
    showStartMonth: false,
    startMonthLabel: moment().format('YYYY-MM'),
    startMonthValue: moment().valueOf(),
    showEndMonth: false,
    endMonthLabel: moment().format('YYYY-MM'),
    endMonthValue: moment().valueOf(),

    form: {
      startDate: moment().format('YYYY-MM-DD'),
      endDate: moment().format('YYYY-MM-DD'),
      type: "1" // 1： 日  2：月
    },
    cardDatas: []
  },

  pageLifetimes: {
    // 页面显示
    show: function () {
      // console.log("页面显示")
      // 获取数据
      this.getStudentCourseCountAcademicData()
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    changeData(title, current, type, lastWeek, lastYear, remark) {

      let title1 = ""; // 同比
      let title2 = "" // 环比
      let tongbi = ""; // 同比数据
      let huanbi = ""; // 环比数据

      // 同比趋势：  0：正常 1：上升  2：下降
      let trend1 = (current - lastWeek) > 0 ? "1" : (current - lastWeek) == 0 ? '0' : "2";
      if (lastWeek <= 0) {
        tongbi = "-"
      }
      else {
        tongbi = parseFloat(
          (
            (Math.abs(current - lastWeek) * 100) /
            lastWeek
          ).toFixed(2)
        ) + "%"
      }
      // 环比趋势：  0：正常 1：上升  2：下降
      let trend2 = (current - lastYear) > 0 ? "1" : (current - lastYear) == 0 ? '0' : "2";
      if (lastYear <= 0) {
        huanbi = "-"
      }
      else {
        huanbi = parseFloat(
          (
            (Math.abs(current - lastYear) * 100) /
            lastYear
          ).toFixed(2)
        ) + "%"
      }
      if (type == 1) {
        // 按日
        title1 = "日同比";
        title2 = "日环比";
      }
      else {
        // 按月
        title1 = "月同比";
        title2 = "月环比";
      }
      return {
        title,
        remark,
        current,
        title1,
        trend1,
        tongbi,
        title2,
        trend2,
        huanbi
      }
    },
    getStudentCourseCountAcademicData() {
      dataModel.getStudentCourseCountAcademicDataApi(this.data.form).then(result => {
        let arr = [
          this.changeData("订单应收金额", result.amount, this.data.radioType, result.amountYear, result.amountWeek, "筛选时间内，经办的所有有效订单应收金额之和"),
          this.changeData("订单实收金额", result.paidAmount, this.data.radioType, result.paidAmountYear, result.paidAmountWeek, "筛选时间内，经办的所有有效订单实收金额之和"),
          this.changeData("退款金额", result.refundAmount, this.data.radioType, result.refundAmountYear, result.refundAmountWeek, "筛选时间内，经办的所有退款订单退款金额之和"),
        ]
        this.setData({
          cardDatas: arr
        })

      })
    },
    onRadioChange(e) {
      this.setData({
        radioType: e.detail
      })
    },
    // 选择开始日期
    tapStartDate() {
      this.setData({
        showStartDate: true
      })
    },
    onCloseStartDate() {
      this.setData({
        showStartDate: false
      })
    },
    chooseStartDate(e) {
      const diff = moment(moment(e.detail).format("YYYY-MM-DD")).diff(moment(this.data.endDateLabel), 'days')
      if (diff > 0) {
        wx.showToast({
          title: '开始日期不能大于结束日期',
          icon: "none",
          duration: 2000
        })
      }
      else {
        this.setData({
          startDateValue: e.detail,
          startDateLabel: moment(e.detail).format("YYYY-MM-DD"),
          showStartDate: false
        })

      }
    },
    // 选择结束日期
    tapEndDate() {
      this.setData({
        showEndDate: true
      })
    },
    onCloseEndDate() {
      this.setData({
        showEndDate: false
      })
    },
    chooseEndDate(e) {
      const diff = moment(moment(e.detail).format("YYYY-MM-DD")).diff(moment(this.data.startDateLabel), 'days')
      if (diff < 0) {
        wx.showToast({
          title: '结束日期不能小于开始日期',
          icon: "none",
          duration: 2000
        })
      }
      else {
        this.setData({
          endDateValue: e.detail,
          endDateLabel: moment(e.detail).format("YYYY-MM-DD"),
          showEndDate: false
        })
      }
    },
    confirmDate() {
      this.setData({
        "form.startDate": this.data.startDateLabel,
        "form.endDate": this.data.endDateLabel,
        "form.type": "1"
      })
      this.getStudentCourseCountAcademicData()
      this.selectComponent('#itemDate').toggle();
    },
    // 选择开始月份
    tapStartMonth() {
      this.setData({
        showStartMonth: true
      })
    },
    onCloseStartMonth() {
      this.setData({
        showStartMonth: false
      })
    },
    chooseStartMonth(e) {
      const diff = moment(moment(e.detail).format("YYYY-MM")).diff(moment(this.data.endMonthLabel), 'months')
      if (diff > 0) {
        wx.showToast({
          title: '开始月份不能大于结束月份',
          icon: "none",
          duration: 2000
        })

      }
      else {
        this.setData({
          startMonthValue: e.detail,
          startMonthLabel: moment(e.detail).format("YYYY-MM"),
          showStartMonth: false
        })
      }
    },
    // 选择结束月份
    tapEndMonth() {
      this.setData({
        showEndMonth: true
      })
    },
    onCloseEndMonth() {
      this.setData({
        showEndMonth: false
      })
    },
    chooseEndMonth(e) {
      const diff = moment(moment(e.detail).format("YYYY-MM")).diff(moment(this.data.startMonthLabel), 'months')
      if (diff < 0) {
        wx.showToast({
          title: '结束月份不能小于开始月份',
          icon: "none",
          duration: 2000
        })
      }
      else {
        this.setData({
          endMonthValue: e.detail,
          endMonthLabel: moment(e.detail).format("YYYY-MM"),
          showEndMonth: false
        })
      }
    },
    confirmMonth() {
      this.setData({
        "form.startDate": this.data.startMonthLabel,
        "form.endDate": this.data.endMonthLabel,
        "form.type": "2"
      })
      this.getStudentCourseCountAcademicData()
      this.selectComponent('#itemDate').toggle();
    },
    showRemark(e) {
      wx.showToast({
        title: e.currentTarget.dataset.query.remark,
        icon: "none",
        duration: 2000
      })
    }
  }
})